package cn.wolfcode.crm.util;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: crm
 * @description:
 * @author: Mr.chen
 * @create: 2018-07-09 14:52
 **/
@ControllerAdvice
public class ExceptionsHandlerUtil {
    /**
     * 指定的异常在该方法中处理
     */
    @ExceptionHandler(UnauthorizedException.class)
    public void handlerException(HttpServletResponse response, HandlerMethod method) throws IOException {
        //判断是普通请求,还是ajax请求(返回json数据)
        ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class);
        if(responseBody!=null){
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write("{\"success\":false, \"msg\":\"没有操作权限\"}");
        }else {
            response.sendRedirect("/nopermission.jsp");
        }
    }
}
